#include "easy_fsmc.h"
#include "unistd.h"

fsm_handle_t *handle =NULL;
typedef enum
{
    E_IDLE,       /* 空闲 */
    E_BELL,       /* 铃声 */
    E_WHITE_LIST, /* 白名单 */
    E_BLACK_LIST, /* 黑名单 */
    E_FINISH,     /* 通话结束 */
    E_BUSY,       /* 占线忙 */
    E_CONNECT,    /* 接通 */
    E_DIAL,       /* 拨号 */
    E_TIME_OUT    /* 超时 */
}Event; /* 触发事件,由外部到来 */


typedef enum
{
    S_IDLE=0, /* 空闲 */
    S_BELL,   /* 响铃 */
    S_TALK,   /* 通话 */
    S_HANGUP, /* 挂断 */
    S_DIAL,   /* 拨号 */
    S_TIMEOUT /* 超时 */
}State; /* 当前状态 */


//限于篇幅，此处展示部分的动作函数
void talk_func(FSMC_EVENT event, void *args)
{
    int ret=0;
    printf("通话中... ");
    printf("【输入1-9, 通话结束】:");
    scanf("%d", &ret);
    easy_fsm_event_set(handle,E_FINISH);
    //*event = E_FINISH; //触发下一个事件
    //此处做演示,直接更新触发事件，
    //实际中可根据传入的参数,进行更新触发事件，
    //或根据外部条件进行触发
}

void hangup_func(FSMC_EVENT event, void *args)
{
    printf("\n挂断电话...\n\n");
    //*event = E_IDLE; //更新触发下一个事件
    easy_fsm_event_set(handle, E_IDLE);
}


//空闲处理函数
void idle_func(FSMC_EVENT event, void *args)
{
    //...
    printf("\n空闲状态...\n\n");
    easy_fsm_event_set(handle, E_BELL);
}
//响铃处理函数
void bell_func(FSMC_EVENT event, void *args)
{
    //...
    printf("\n电话响了...\n\n");
    easy_fsm_event_set(handle, E_WHITE_LIST);
}
//拨号处理函数
void dial_func(FSMC_EVENT event, void *args)
{
    //...
    printf("\n开始通话...\n\n");
    easy_fsm_event_set(handle,E_CONNECT);
}

//
void timeout_func(FSMC_EVENT event, void *args)
{
    //...
}

fsm_status_t fsmtb[] = {
    /* 事件         当前状态        动作        下一个状态 */
    { E_IDLE,       S_IDLE,     idle_func,      S_IDLE , NULL},
    { E_BELL,       S_IDLE,     bell_func,      S_BELL , NULL}, 
    { E_DIAL,       S_IDLE,     dial_func,      S_DIAL , NULL},
    { E_TIME_OUT,   S_DIAL,     timeout_func,   S_TIMEOUT , NULL},
    { E_TIME_OUT,   S_BELL,     timeout_func,   S_TIMEOUT , NULL},
    { E_BUSY,       S_DIAL,     hangup_func,    S_HANGUP , NULL},
    { E_CONNECT,    S_DIAL,     talk_func,      S_TALK , NULL},
    { E_WHITE_LIST, S_BELL,     talk_func,      S_TALK , NULL}, 
    { E_BLACK_LIST, S_BELL,     hangup_func,    S_HANGUP , NULL},
    { E_FINISH,     S_TALK,     hangup_func,    S_HANGUP , NULL},
    { E_IDLE,       S_HANGUP,   idle_func,      S_IDLE , NULL},
    { E_IDLE,       S_TIMEOUT,  idle_func,      S_IDLE , NULL }
};



int main(void)
{ 
    int num = sizeof(fsmtb)/sizeof(fsmtb[0]);
    handle = easy_fsm_init(fsmtb, S_IDLE, E_IDLE, num);
    while(1)
    {
        easy_fsm_process(handle); //循环运行
        sleep(1);
    }
    
    return 0;
}